--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.
--Data Schema For Version 0.0.0.0
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoDelete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoDelete]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoDelete]
(
	@Original_CustomerID NChar(5),
	@Original_CustomerTypeID NChar(10)
)
AS
SET NOCOUNT OFF;

DELETE FROM 
	[CustomerCustomerDemo] 
WHERE 
	[CustomerID] = @Original_CustomerID AND
	[CustomerTypeID] = @Original_CustomerTypeID ;

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectByCustomerCustomerDemoPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectByCustomerCustomerDemoPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectByCustomerCustomerDemoPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM 
[CustomerCustomerDemo]
WHERE

	[CustomerCustomerDemo].[CustomerID] IN (SELECT [CustomerID] 
											FROM OpenXML(@hDoc, '//CustomerCustomerDemo', 2) 
											WITH ([CustomerID] char(36) 'CustomerID')) 

	AND

	[CustomerCustomerDemo].[CustomerTypeID] IN (SELECT [CustomerTypeID] 
											FROM OpenXML(@hDoc, '//CustomerCustomerDemo', 2) 
											WITH ([CustomerTypeID] char(36) 'CustomerTypeID')) 


exec sp_xml_removeDocument @hDoc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectByCustomerCustomerDemoSinglePk]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectByCustomerCustomerDemoSinglePk]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectByCustomerCustomerDemoSinglePk]
(
	@CustomerID NChar(5),
	@CustomerTypeID NChar(10)
)
AS

SELECT 
	[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM
[CustomerCustomerDemo]
WHERE 
[CustomerCustomerDemo].[CustomerID] = @CustomerID AND [CustomerCustomerDemo].[CustomerTypeID] = @CustomerTypeID 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoInsert]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoInsert]
(
	@CustomerTypeID NChar(10) = default,
	@CustomerID NChar(5) = default,
	@created_date datetime,
	@created_by varchar (50),
	@modified_by varchar (50)

)
AS
SET NOCOUNT OFF;

if (@created_date IS NULL)
SET @created_date = GetDate()

INSERT INTO [CustomerCustomerDemo]
(
[CustomerID],
[CustomerTypeID],
[created_date],
[created_by],
[modified_date],
[modified_by]

)
VALUES
(
	@CustomerID,
	@CustomerTypeID,
	@created_date,
	@created_by,
	@created_date,
	@modified_by

);


SELECT 
	[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM
[CustomerCustomerDemo]
WHERE
	[CustomerCustomerDemo].[CustomerID] = @CustomerID AND
	[CustomerCustomerDemo].[CustomerTypeID] = @CustomerTypeID;
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoPagingSelect]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoPagingSelect]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoPagingSelect]
(
	@page int, -- page number selected by the user
	@pageSize int, -- number of items on the page
	@orderByColumn varchar(100), -- name of column to order things by
	@ascending bit, -- order column ascending or descending
	@filter varchar(100) = null, -- filter statement passed in to determine like criteria on order by column
	@count int out -- number of items in the collection
)
AS

SET NOCOUNT ON;


CREATE TABLE #tmpTable
(
[CustomerID] NChar(5),
[CustomerTypeID] NChar(10)
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@pageSize * @page)
IF (@total__ivqatedr <> 0)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[customerid],
	[customertypeid]
)
SELECT
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[customertypeid]
FROM
[CustomerCustomerDemo]
WHERE
	(@orderByColumn = 'CustomerTypeID' and (((@filter is null) or ([CustomerCustomerDemo].[CustomerTypeID] is null)) or (@filter is not null and [CustomerCustomerDemo].[CustomerTypeID] LIKE @filter)))
or
	(@orderByColumn = 'CustomerID' and (((@filter is null) or ([CustomerCustomerDemo].[CustomerID] is null)) or (@filter is not null and [CustomerCustomerDemo].[CustomerID] LIKE @filter)))
ORDER BY
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END

-- set @count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @count = (
SELECT count(*)
FROM
[CustomerCustomerDemo]
WHERE
	(@orderByColumn = 'CustomerTypeID' and (((@filter is null) or ([CustomerCustomerDemo].[CustomerTypeID] is null)) or (@filter is not null and [CustomerCustomerDemo].[CustomerTypeID] LIKE @filter)))
or
	(@orderByColumn = 'CustomerID' and (((@filter is null) or ([CustomerCustomerDemo].[CustomerID] is null)) or (@filter is not null and [CustomerCustomerDemo].[CustomerID] LIKE @filter)))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@pageSize * @page) - @pageSize
IF (@total__ivqatedr <> 0)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
SET ROWCOUNT @pageSize
SELECT
	[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [CustomerCustomerDemo] ON #tmpTable.[customerid] = [CUSTOMERCUSTOMERDEMO].[customerid] AND
#tmpTable.[customertypeid] = [CUSTOMERCUSTOMERDEMO].[customertypeid]
ORDER BY
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END

DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectBySearchAnd]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectBySearchAnd]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectBySearchAnd]
(
	@CustomerTypeID NChar(10) = null, 
	@CustomerID NChar(5) = null, 
	@paging_PageIndex int = -1, -- page number selected by the user
	@paging_RecordsperPage int = -1, -- number of items on the page
	@paging_OrderByColumn varchar(100) = '', -- name of column to order things by
	@paging_Ascending bit = 1, -- order column ascending or descending
	@paging_Count int out -- number of items in the collection
)
AS

CREATE TABLE #tmpTable 
(
	[CustomerID] NChar(5),
[CustomerTypeID] NChar(10)
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex)
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[customerid],
	[customertypeid]
)
SELECT
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[customertypeid]
FROM
[CustomerCustomerDemo]
WHERE
((@CustomerTypeID is null) OR ([CustomerCustomerDemo].[CustomerTypeID] LIKE @CustomerTypeID)) AND
((@CustomerID is null) OR ([CustomerCustomerDemo].[CustomerID] LIKE @CustomerID))
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END
-- set @paging_Count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @paging_Count = (
SELECT count(*)
FROM
[CustomerCustomerDemo]
WHERE
((@CustomerTypeID is null) OR ([CustomerCustomerDemo].[CustomerTypeID] LIKE @CustomerTypeID)) AND
((@CustomerID is null) OR ([CustomerCustomerDemo].[CustomerID] LIKE @CustomerID))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex) - @paging_RecordsperPage
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
IF (@paging_PageIndex <> -1)
BEGIN
SET ROWCOUNT @paging_RecordsperPage
END
SELECT
	[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [CustomerCustomerDemo] ON #tmpTable.[customerid] = [CUSTOMERCUSTOMERDEMO].[customerid] AND
#tmpTable.[customertypeid] = [CUSTOMERCUSTOMERDEMO].[customertypeid]
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END
DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoUpdate]
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoUpdate]
(
	@modified_by varchar (50),
	@modified_date datetime = null,
	@Original_CustomerID NChar(5),
	@Original_CustomerTypeID NChar(10),
	@Original_time_stamp timestamp

)
AS

IF (@modified_date IS NULL)
SET @modified_date = GetDate();

SET NOCOUNT OFF;
UPDATE 
[CustomerCustomerDemo] 
SET
[modified_by] = @modified_by,
[modified_date] = @modified_date

WHERE
	[CustomerCustomerDemo].[CustomerID] = @Original_CustomerID AND
	[CustomerCustomerDemo].[CustomerTypeID] = @Original_CustomerTypeID AND
	[CustomerCustomerDemo].[time_stamp] = @Original_time_stamp


SELECT
	[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]
FROM 
[CustomerCustomerDemo]
WHERE
	[CustomerCustomerDemo].[CustomerID] = @Original_CustomerID AND
	[CustomerCustomerDemo].[CustomerTypeID] = @Original_CustomerTypeID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectByCreatedDateRange]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectByCreatedDateRange]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectByCreatedDateRange]
(
	@start_date datetime,
	@end_date datetime
)
AS

SET NOCOUNT ON;

SELECT
		[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM
[CustomerCustomerDemo]
WHERE
((([CustomerCustomerDemo].[created_date] IS NULL) AND (@start_date IS NULL)) OR (@start_date <= [CustomerCustomerDemo].[created_date])) AND 
((([CustomerCustomerDemo].[created_date] IS NULL) AND (@end_date IS NULL)) OR (@end_date >= [CustomerCustomerDemo].[created_date]))
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectByModifiedDateRange]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectByModifiedDateRange]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectByModifiedDateRange]
(
	@start_date datetime,
	@end_date datetime
)
AS

SET NOCOUNT ON;

SELECT
		[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM
[CustomerCustomerDemo]
WHERE
((([CustomerCustomerDemo].[modified_date] IS NULL) AND (@start_date IS NULL)) OR (@start_date <= [CustomerCustomerDemo].[modified_date])) AND 
((([CustomerCustomerDemo].[modified_date] IS NULL) AND (@end_date IS NULL)) OR (@end_date >= [CustomerCustomerDemo].[modified_date]))
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectBySearchOr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectBySearchOr]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectBySearchOr]
(
	@CustomerTypeID NChar(10) = null, 
	@CustomerID NChar(5) = null, 
	@paging_PageIndex int = -1, -- page number selected by the user
	@paging_RecordsperPage int = -1, -- number of items on the page
	@paging_OrderByColumn varchar(100) = '', -- name of column to order things by
	@paging_Ascending bit = 1, -- order column ascending or descending
	@paging_Count int out -- number of items in the collection
)
AS

CREATE TABLE #tmpTable 
(
	[CustomerID] NChar(5),
[CustomerTypeID] NChar(10)
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex)
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[customerid],
	[customertypeid]
)
SELECT
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[customertypeid]
FROM
[CustomerCustomerDemo]
WHERE
((@CustomerTypeID is null) OR ([CustomerCustomerDemo].[CustomerTypeID] LIKE @CustomerTypeID)) OR
((@CustomerID is null) OR ([CustomerCustomerDemo].[CustomerID] LIKE @CustomerID))
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END
-- set @paging_Count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @paging_Count = (
SELECT count(*)
FROM
[CustomerCustomerDemo]
WHERE
((@CustomerTypeID is null) OR ([CustomerCustomerDemo].[CustomerTypeID] LIKE @CustomerTypeID)) OR
((@CustomerID is null) OR ([CustomerCustomerDemo].[CustomerID] LIKE @CustomerID))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex) - @paging_RecordsperPage
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
IF (@paging_PageIndex <> -1)
BEGIN
SET ROWCOUNT @paging_RecordsperPage
END
SELECT
	[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [CustomerCustomerDemo] ON #tmpTable.[customerid] = [CUSTOMERCUSTOMERDEMO].[customerid] AND
#tmpTable.[customertypeid] = [CUSTOMERCUSTOMERDEMO].[customertypeid]
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerTypeID' THEN [CustomerCustomerDemo].[CustomerTypeID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'CustomerID' THEN [CustomerCustomerDemo].[CustomerID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [CustomerCustomerDemo].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [CustomerCustomerDemo].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [CustomerCustomerDemo].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [CustomerCustomerDemo].[modified_date] END END
DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelect]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelect]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelect]
AS

SET NOCOUNT ON;

SELECT
		[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM 
[CustomerCustomerDemo]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectByCustomerDemographicPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectByCustomerDemographicPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectByCustomerDemographicPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM 
[CustomerCustomerDemo]
WHERE
	[CustomerCustomerDemo].[CustomerTypeID] IN (SELECT [CustomerTypeID]
				FROM OpenXML(@hDoc, '//CustomerDemographics', 2)
				WITH ([CustomerTypeID] NChar(10) 'CustomerTypeID'))

exec sp_xml_removeDocument @hDoc	

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_CustomerCustomerDemoSelectByCustomerPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_CustomerCustomerDemoSelectByCustomerPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE PROCEDURE [dbo].[gen_CustomerCustomerDemoSelectByCustomerPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[CustomerCustomerDemo].[customertypeid],
	[CustomerCustomerDemo].[customerid],
	[CustomerCustomerDemo].[created_by],
	[CustomerCustomerDemo].[created_date],
	[CustomerCustomerDemo].[modified_by],
	[CustomerCustomerDemo].[modified_date],
	[CustomerCustomerDemo].[time_stamp]

FROM 
[CustomerCustomerDemo]
WHERE
	[CustomerCustomerDemo].[CustomerID] IN (SELECT [CustomerID]
				FROM OpenXML(@hDoc, '//Customers', 2)
				WITH ([CustomerID] NChar(5) 'CustomerID'))

exec sp_xml_removeDocument @hDoc	

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

